//
//  CZComposeViewController.swift
//  GZWeibo666
//
//  Created by 魔龙 on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

enum CZComposeViewControllerType: Int {
    case Forward = 1  // 转发控制器
    case Comment = 2  // 评论控制器
}

class CZComposeViewController: UIViewController {
    /// 控制器类型
    private var controllerType: CZComposeViewControllerType
    
    var status : CZStatus?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(controllerType: CZComposeViewControllerType){
        self.controllerType = controllerType
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        prepareUI()
        
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 导航栏
        ComposeNavigationBar()
        // 文本窗口
        ComposeTextView()
    }
    // 导航栏
    private func ComposeNavigationBar(){
        // 左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        // 右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: "sendStatus")
        // 导航栏标题
        titleView()
    }
    
    /// 导航栏标题
    private func titleView(){
        var titleText = "发微博"
        if controllerType == CZComposeViewControllerType.Forward{
            titleText = "转发微博"
        }else if controllerType == CZComposeViewControllerType.Comment{
            titleText = "评论微博"
        }
       // 获取用户名
        if let name = CZUserAccount.loadUserAccount()?.screen_name{
            // 标题Label
            let textLabel = UILabel(frame: CGRectMake(0, -10, 100, 44))
            textLabel.text = titleText
            textLabel.font = UIFont.systemFontOfSize(16)
            textLabel.textAlignment = NSTextAlignment.Center

            // 名字Label
            let nameLabel = UILabel(frame: CGRectMake(0, 10, 100, 44))
            nameLabel.text = name
            nameLabel.font = UIFont.systemFontOfSize(13)
            nameLabel.textAlignment = NSTextAlignment.Center
            nameLabel.textColor = UIColor.grayColor()
            
            let titleView = UIView(frame: CGRectMake(0, 0, 100, 44))
            titleView.addSubview(textLabel)
            titleView.addSubview(nameLabel)
            navigationItem.titleView = titleView
        }else {
            // 没有用户名
            navigationItem.title = titleText
        }
    }
    
    // 文本窗口
    private func ComposeTextView(){
        // 添加子控件
        view.addSubview(textView)
        // 添加约束
        textView.ff_Fill(view)
    }
 
    
    
    /// 关闭控制器
    func close() {
        
        // 关闭键盘
        textView.resignFirstResponder()
        
        // 关闭sv提示
        SVProgressHUD.dismiss()
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    // 发送数据
    func sendStatus() {
        print("发送")
        // 获取文本框的内容
        let text = textView.text
        
        if text == nil || text == "" {
            SVProgressHUD.showInfoWithStatus("内容不能不空")
            return
        }
        
        if controllerType == CZComposeViewControllerType.Forward{
            print("转发微博")
            
            CZNetworkTool.sharedInstance.sendRepostStatus(status?.id, repostContent: text, finished: { (result, error) -> () in
                print("\(result)")
            })
            self.close()
        }else if controllerType == CZComposeViewControllerType.Comment{
            print("评论微博")
            
            CZNetworkTool.sharedInstance.loadCommentCretae(text, id: (status?.id)!, finished: { (result, error) -> () in
                print("\(result)")
            })
            self.close()
        }
        
        
    }
    
    // MARK - 懒加载
    /// 文本框
    private lazy var textView: UITextView = {
        let textView = UITextView()
        
        textView.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        textView.font = UIFont.systemFontOfSize(16)
        textView.backgroundColor = UIColor(white: 0.85, alpha: 1)
        textView.textColor = UIColor.blackColor()
        textView.bounces = true
        textView.alwaysBounceVertical = true
        
        return textView
    }()
}

